package org.example.exampreparationtreasure.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.exampreparationtreasure.service.QuestionSelectorService;
import org.example.exampreparationtreasure.mapper.QuestionMapper;
import org.example.exampreparationtreasure.model.entity.ExamPaperConfig;
import org.example.exampreparationtreasure.model.entity.Question;
import org.example.exampreparationtreasure.model.entity.PracticeMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 章节练习题目选择器
 * 按照教材章节顺序选择题目
 */

@Service
public class ChapterQuestionSelectorServiceImpl implements QuestionSelectorService {

    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public List<Question> selectQuestions(ExamPaperConfig config, Long userId) {
        if (config.getCategoryId() == null) {
            throw new IllegalArgumentException("章节练习必须指定categoryId");
        }

        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", config.getCategoryId())
                .eq("difficulty", getDifficultyLevel(config.getInitialDifficulty()))
                .orderByAsc("question_id")
                .last("LIMIT " + config.getQuestionCount());

        return questionMapper.selectList(queryWrapper);
    }


    private String getDifficultyLevel(Integer initialDifficulty) {
        switch (initialDifficulty) {
            case 1: return "易";
            case 2: return "中";
            case 3: return "难";
            default: return "易";
        }

    }
}
